import Taro from "@tarojs/taro";
var Utils = {
    //获取时间 _getTime(-7)前七天 2020-03-07
    getTime: function(day, type) {
        var today = new Date();
        if (day) {
            var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
            today.setTime(targetday_milliseconds);
            var time = type ?  " " + handleTime(today.getHours()) + ":" + handleTime(today.getMinutes()) : " 00:00:00";
        } else {
            if (type) {
            today.setTime(today.getTime() + 1000 * 60 * 5);
                var time = " " + handleTime(today.getHours()) + ":" + handleTime(today.getMinutes()) + ":00";
            } else {
                var time = " " + handleTime(today.getHours()) + ":" + handleTime(today.getMinutes()) + ":" +  handleTime(today.getSeconds());
            }
        }
        return today.getFullYear() + "-" + handleTime(today.getMonth() + 1) + "-" + handleTime(today.getDate()) + time;
    },
    getDate: function(day, curTime) {
        var today = new Date();
        if (curTime) today.setTime(curTime);
        if (day) {
            var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
            today.setTime(targetday_milliseconds);
        }
        return today.getFullYear() + "-" + handleTime(today.getMonth() + 1) + "-" + handleTime(today.getDate());
    },
    setDate: function(day) {
        var today = new Date();
        if (!day) return today;
        var dateList = day.split("-");
        today.setFullYear(parseInt(dateList[0]));
        today.setMonth(parseInt(dateList[1]) - 1);
        today.setDate(parseInt(dateList[2]));
        return today.getTime();
    },
    getDate1: function(day, curTime) {
        var dayList = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
        var today = new Date();
        if (curTime) today.setTime(curTime);
        if (day) {
            var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
            today.setTime(targetday_milliseconds);
        }
        var time = today.getFullYear() + "-" + handleTime(today.getMonth() + 1) + "-" + handleTime(today.getDate()),
            date = dayList[today.getDay()];
        return {time: time, date: date};
    },
    transformToPx: function(size) {
        var transSize = 0;
        Taro.getSystemInfo({
            success: res => {
                var width = res.windowWidth;
                if (process.env.TARO_ENV === 'weapp') {
                    transSize = size * (width / 750);
                } else  { //if (process.env.TARO_ENV === 'h5') 
                    if (width >= 640) {
                        var baseSize = 40
                    } else if (width <= 320) {
                        var baseSize = 20
                    } else {
                        var baseSize = width/320*20
                    }
                    transSize = Math.ceil(parseFloat(Taro.pxTransform(size)) * baseSize * 10000) / 10000 ;
                }
            }
        })
        return transSize;
    }
}
function handleTime(month) {
    if (month.toString().length == 1) {
        return "0" + month;
    }
    return month;
}
export default Utils 